/*
 * cpu.c
 *
 *  Created on: Feb 25, 2012
 *      Author: Holly Beach
 */
#include <stdio.h>
#include "cpu.h"
#include "process.h"

int numberOfProcesses;
process processes[1];
process currentProc;
memory mem;
int pc;
int i;

char schedulingPolicy;

void initializeCPU(process procs[], int numProcess, char schedPolicy){
	numberOfProcesses = numProcess;
	processes[0] = procs[0];
	schedulingPolicy = schedPolicy;
	pc = 0;
	i = 0;
	mem.value = 0;
}

void runCPU(){
	currentProc = getProcess(processes,schedulingPolicy);
	currentProc.status = RUNNING;
	currentProc.wasLastProcessRun = 1;
	switch(currentProc.name){
	case UI:
		runUIProcess(&currentProc);
		break;
	case CALCULATOR:
		runCalcProcess(&currentProc);
		break;
	case PRODUCER:
		runProducerProcess(&currentProc);
		break;
	case CONSUMER:
		runConsumerProcess(&currentProc);
		break;
	}
	printProcessStates(processes);

}


int runUIProcess(process* proc){
	pc = proc->pc;
	while (pc < 1000){
		if (pc == proc->addressForSyscall){
			printf("Process %d (%s) did something\n", proc->id, proc->strName);
		}
		pc++;
	}
	return 0;
}
int runCalcProcess(process* proc){
	pc = proc->pc;
		while (pc < 1000){
			if (pc == proc->addressForSyscall){
				printf("Process %d (%s) did something\n", proc->id, proc->strName);
			}
			pc++;
		}
		return 0;
}
int runProducerProcess(process* proc){
	pc = proc->pc;
	while (pc < 10000) {
		if (pc == proc->addressForSyscall){
			printf("Process %d (%s) attempted to increment memory at address 0\n",
					proc->id, proc->strName);
			incrementMemValue(&mem, proc);
		}
		pc++;
	}
	return 0;
}
int runConsumerProcess(process* proc){
	pc = proc->pc;
		while (pc < 10000) {
			if (pc == proc->addressForSyscall){
				printf("Process %d (%s) attempted to decrement memory at address 0\n",
						proc->id, proc->strName);
				decrementMemValue(&mem, proc);
			}
			pc++;
		}
		return 0;
}
